package com.lck.blogmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lck.blogmanager.Enum.TagTypeEnum;
import com.lck.blogmanager.common.BasePageReq;
import com.lck.blogmanager.entity.TagEntity;
import com.lck.blogmanager.mapper.TagMapper;
import com.lck.blogmanager.req.AddTagReq;
import com.lck.blogmanager.req.DeleteTagReq;
import com.lck.blogmanager.service.TagService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/4/8 16:34
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class TagServiceImpl extends ServiceImpl<TagMapper, TagEntity> implements TagService {


    @Override
    public Integer addTag(AddTagReq req) {
        TagEntity entity = new TagEntity();
        entity.setType(req.getType())
                .setTagName(req.getTagName());
        this.save(entity);
        return entity.getId();
    }

    @Override
    public void deleteTag(DeleteTagReq req) {
        this.getBaseMapper().deleteById(req.getId());
    }

    @Override
    public Page<TagEntity> listTag(BasePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        QueryWrapper<TagEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByAsc(TagEntity::getId);
        Page<TagEntity> result = this.page(page,wrapper);
        return result;
    }

    @Override
    public List<TagEntity> getQuestionTag() {
        QueryWrapper<TagEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TagEntity::getType, TagTypeEnum.QUESTION.getValue());
        List<TagEntity> tagEntities = this.getBaseMapper().selectList(wrapper);
        return tagEntities;
    }

    @Override
    public List<TagEntity> getBlogTag() {
        QueryWrapper<TagEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TagEntity::getType, TagTypeEnum.BLOG.getValue());
        List<TagEntity> tagEntities = this.getBaseMapper().selectList(wrapper);
        return tagEntities;
    }
}
